<template>
  <div v-if="dataset">
    <el-row>
      <el-col :span="22">
        <input
          v-if="auditData.data.length == 0 || auditData.data[0].FAUDIT != 0"
          class="input_inner"
          placeholder="请输入电子监管码"
          v-model="supervisionInput"
          @keyup.enter="querySupervisionCode"
          @focus="focus($event)"
          ref="searchInput"
        />
      </el-col>

      <el-col :span="2">
        <el-button
          v-if="auditData.data.length == 0 || auditData.data[0].FAUDIT != 0"
          type="primary"
          style="margin: 34px 0 0 -20px"
          @click="clearTable(dataset.data)"
          >清空</el-button
        >
      </el-col>
    </el-row>
    <div style="height: 360px">
      <el-table
        ref="table"
        :data="dataset.data"
        max-height="360"
        style="width: 100%"
      >
        <el-table-column type="index" width="50" />
        <el-table-column prop="FSUPERVISIONCODE" label="电子监管码" />
        <el-table-column prop="FBARCODE" label="条形码" />
        <el-table-column prop="FDESC" label="通用名称" />
        <el-table-column
          v-if="auditData.data.length == 0 || auditData.data[0].FAUDIT != 0"
          fixed="right"
          label="操作"
          width="80"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              title="删除"
              size="small"
              @click="remove(scope.row)"
            >
              <i class="el-icon-circle-close"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import baseView from "@/views/base";
// import { cloneDeep } from "lodash";

// import datasetCfg from "./dataset";
export default {
  components: {},
  extends: baseView,
  props: {
    dataset: Object,
    codeData: Object,
    auditData: Object,
    kind: String,
    subDataset: Object,
  },
  data() {
    return {
      supervisionInput: null,
      // curRecData: [],
      // dataset: null,
      // datasetCfg: datasetCfg,
    };
  },
  methods: {
    querySupervisionCode() {
      var val = this.supervisionInput;
      if (!val.trim()) return;
      //处理商品组合
      if (this.kind == "combine") {
        var curRec = this.dataset.data.find((item, index, arr) => {
          return item["FSUPERVISIONCODE"] == val;
        });
        if (curRec) {
          this.$message.error("【" + val + "】电子监管码已存在");
          this.$refs.searchInput.select();
          return;
        }
        if (this.codeData.data.length == 0) {
          this.dataset.add({
            FSUPERVISIONCODE: val,
          });
        } else {
          this.dataset.add({
            FSUPERVISIONCODE: val,
            FBARCODE: this.codeData.data[0].FBARCODE,
            FDESC: this.codeData.data[0].FDESC,
          });
        }
        this.$refs.searchInput.select();
      } else {
        /*   var curRec = this.dataset.data.find((item, index, arr) => {
            return item["FSUPERVISIONCODE"] == supcode;
          }); */
        /*  var curRec2 = this.dataset.data.find((item, index, arr) => {
            return arr.length == this.codeData["FAMOUNT"];
          }); */
        for (var i = 0; i < this.subDataset.data.length; i++) {
          //this.subDataset.setCurRecord(this.subDataset.data[i]);
          debugger;
          let data = this.subDataset
            .getModel(this.subDataset.data[i])
            .getChildStore("SupervisioncodeEntity_in").data;
          let curRec = data.find((item) => {
            return item["FSUPERVISIONCODE"] == val;
          });
          if (curRec) {
            this.$message.error("【" + val + "】电子监管码已存在");
            this.$refs.searchInput.select();
            return;
          }
        }
        this.dataset.add({
          FSUPERVISIONCODE: val,
          FBARCODE: this.codeData["FBARCODE"],
          FDESC: this.codeData["FDESC"],
        });

        this.$refs.searchInput.select();
      }
    },
    focus(event) {
      event.currentTarget.select();
    },
    remove(data) {
      debugger;
      this.dataset.remove(data);
    },
    clearTable(data) {
      // debugger;
      this.dataset.remove(data);
    },
  },
  mounted() {
    // debugger;
    //创建主数据集
    // this.dataset = this.$dsContainer.createDataSet(this.datasetCfg);
    // this.curRecData = cloneDeep(this.dataset.data);
  },
};
</script>
<style rel="stylesheet/scss" lang="scss" scope>
.input_inner {
  line-height: 36px;
  margin: 0 0 20px 50px;
  padding-left: 10px;
  outline: none;
}
</style>
